package com.qdu.program.controller;

import com.qdu.program.entity.Collections;
import com.qdu.program.entity.Read;
import com.qdu.program.entity.User;
import com.qdu.program.entity.Userinfo;
import com.qdu.program.serviceImpl.CollectionsServiceImpl;
import com.qdu.program.serviceImpl.ReadServiceImpl;
import com.qdu.program.serviceImpl.UserServiceImpl;
import com.qdu.program.serviceImpl.UserinfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    UserinfoServiceImpl userinfoService;

    @Autowired
    ReadServiceImpl readService;

    @Autowired
    CollectionsServiceImpl collectionsService;

    //注册
    @RequestMapping("/register")
    public String Register(String userNumber,String password) {
//        System.out.println("1");
        if(userService.selectByPrimaryKey(userNumber)==null){
            Userinfo userinfo = new Userinfo();
            User user = new User();
            user.setUserNumber(userNumber);
            user.setUserPassword(password);
            userinfo.setUserNumber(user.getUserNumber());
            userService.insert(user);
            userinfoService.insertSelective(userinfo);
            return "registerSuccess";
//            userinfo.setUserName(user.getUserName());
        }else {
            return "registerFailed";
        }
    }

    //登录
    @RequestMapping("/login")
    public String login(String userNumber, String password) {
//        System.out.println("interSucess");
        User user = userService.selectByPrimaryKey(userNumber);
        if(user!=null){
            if(user.getUserPassword().equals(password)){
                return "loginSuccess";
            }else{
                return "loginFailed";
            }
        }else {
            return "用户不存在!";
        }
    }

    //查询一个用户的所有收藏文章
    @RequestMapping("/getAllCollections")
    public List<Read> getAllCollections(String userNumber){
        List<Collections> collectionsList = collectionsService.selectAllCollections(userNumber);
        List<Read> readList = new ArrayList<>();
//        System.out.println(collectionsList.get(0));
        for(Collections c:collectionsList){
            readList.add(readService.selectByPrimaryKey(c.getReadId()));
        }
//        System.out.println(readList.size());
        return readList;
    }

    //随机挑选10篇文章
    @RequestMapping("/getTenRead")
    public List<Read> randomRead(){
        List<Read> readList = readService.selectTenRead();
//        System.out.println(readList.get(1).getReadText());
        return readList;
    }

    //通过文章id获取一片文章
    @RequestMapping("/getOneRead")
    public Read getOneRead(Integer id){
        Read read = readService.selectByPrimaryKey(id);
        return read;
    }

    //更新个人信息
    @RequestMapping("/updateUserinfo")
    public String updateUserInfo(String userNumber, String userName, String userinfoEmail, String userinfoTel,String userinfoGender){
        Userinfo userinfo = new Userinfo();
        userinfo.setUserNumber(userNumber);
        userinfo.setUserName(userName);
        userinfo.setUserinfoEmail(userinfoEmail);
        userinfo.setUserinfoTel(userinfoTel);
        userinfo.setUserinfoGender(userinfoGender);
        userinfoService.updateByPrimaryKey(userinfo);
        return "upDateSuccess";
    }


//    @RequestMapping("/getReadByReadId")
//    public Read getReadByReadId(Integer readId){
//        return readService.selectByPrimaryKey(readId);
//    }

    //添加收藏
    @RequestMapping("/addCollection")
    public String addCollection(String userNumber,Integer readId){
//        System.out.println(userNumber+"====="+readId);
        List<Collections> list = collectionsService.selectAllCollections(userNumber);
        Collections collections = new Collections();
        collections.setUserNumber(userNumber);
        collections.setReadId(readId);
        int i = 0;
        for(Collections c:list){
            if(c.getReadId()==readId&&c.getUserNumber().equals(userNumber)){
                i=1;
            }
        }
        if(i==1){
            return "已收藏该文章";
        }else {
            collectionsService.insert(collections);
            return "collectSuccess";
        }
    }

    @RequestMapping("/deleteCollection")
    public String deleteCollection(String userNumber,Integer readId){
        Integer colId = 0;
        List<Collections> collectionsList = collectionsService.selectAllCollections(userNumber);
        for(Collections c:collectionsList){
            if(c.getReadId()==readId){
                colId=c.getColId();
                collectionsService.deleteByPrimaryKey(colId);
                return "deleteSuccess";
            }
        }
        return "deleteFailed";
    }

    @RequestMapping("/getUserInfo")
    public Userinfo getUserInfo(String userNumber){
        Userinfo userInfo = new Userinfo();
        userInfo = userinfoService.selectByPrimaryKey(userNumber);
        return userInfo;
    }

    @RequestMapping("/addRead")
    public String addRead(String userNumber,String text){
        Read read = new Read();
        read.setUserNumber(userNumber);
        read.setReadText(text);
        readService.insert(read);
        return "addSuccess";
    }

//    @RequestMapping("/getMyRead")
//    public List<Read> getMyRead(String userNumber){
//        List<Read> readList = readService.
//    }

    @RequestMapping("/getReadByUserNumber")
    public List<Read> getReadByUserNumber(String userNumber){
        List<Read> readList = new ArrayList<>();
        readList = readService.selectReadByUserNumber(userNumber);
        return readList;
    }


    @RequestMapping("/deleteMyRead")
    public String deleteMyRead(Integer readId, String userNumber){
         collectionsService.deleteByReadId(readId);
         readService.deleteByPrimaryKey(readId);
        System.out.println("hello");
         return "deleteSuccess";
    }
}
